package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.DirectoryMapper;
import com.bonc.ioc.service.DirectoryService;
import com.bonc.ioc.vo.DirectoryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/18 11:06
 * @change : 2021/3/18 11:06 by q8541q@163.com  for init
 */
@Service
public class DirectoryServiceImpl extends BaseService implements DirectoryService {

    @Resource
    private DirectoryMapper directoryMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return directoryMapper.delete(id);
    }

    @Override
    public int save(DirectoryVo record) {
        if (record.getId() != null) {
            fillModifyTimeInfo(record);
            return directoryMapper.update(record);
        } else {
            if (record.getCreateTime() == null) {
                fillBasicTimeInfo(record);
            }
            fillModifyTimeInfo(record);
            record.setCode(String.valueOf(System.currentTimeMillis()));
            return directoryMapper.save(record);
        }

    }

    @Override
    public DirectoryVo selectByPrimaryKey(Long id) {
        return directoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DirectoryVo> getDirs() {
        return directoryMapper.getDirs();
    }

    private void fillModifyTimeInfo(DirectoryVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(DirectoryVo record) {
        record.setCreateTime(new Date());
    }
}
